package com.applink.sell_proof;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewStub;
import android.view.WindowManager;
import android.widget.CompoundButton;

import com.actionbarsherlock.app.ActionBar;
import com.applink.sell_proof.constant.IntentKey;
import com.applink.sell_proof.dialog.EAlertDialog;
import com.applink.sell_proof.dialog.WaitingDialog;

public abstract class BaseActionBarActivity extends AbstractActivity {

	protected com.actionbarsherlock.app.ActionBar mActionbar;
	private int mPreviousActivityKey;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// call super constructor
		super.onCreate(savedInstanceState);

		// Inflating the layout avoid with the local inflating logic
		super.setContentView(R.layout.main_normal);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
		mActionbar = getSupportActionBar();
		mActionbar.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.bg_topbar));
		mActionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

		// set selected tab for highlight the bottom bar
		mPreviousActivityKey = getIntent().getIntExtra(IntentKey.ACTIVITY_NAME,
				0);
		switch (mPreviousActivityKey) {
		case BasePagerActivity.SELL_ACTIVITY:
			((CompoundButton) findViewById(R.id.deal_tab)).setChecked(true);
			break;
		case BasePagerActivity.REWARD_ACTIVITY:
			((CompoundButton) findViewById(R.id.reward_tab)).setChecked(true);
			break;
		case BasePagerActivity.LEARN_ACTIVITY:
			((CompoundButton) findViewById(R.id.learn_tab)).setChecked(true);
			break;
		case BasePagerActivity.LIFE_ACTIVITY:
			((CompoundButton) findViewById(R.id.announce_tab)).setChecked(true);
			break;
		case BasePagerActivity.SUPPORT_ACTIVITY:
			((CompoundButton) findViewById(R.id.custservice_tab))
					.setChecked(true);
			break;
		default:
			break;
		}
	}

	public int getTabActivityIndex() {
		return mPreviousActivityKey;
	}

	@Override
	public void setContentView(int layoutResId) {
		ViewStub stub = (ViewStub) findViewById(R.id.main_stub);
		stub.setLayoutResource(layoutResId);
		stub.inflate();

		findViews();
	}

	/***
	 * binding all the variables to it View value
	 */
	@Override
	protected void findViews() {
		findViewById(R.id.deal_tab).setOnClickListener(this);
		findViewById(R.id.reward_tab).setOnClickListener(this);
		findViewById(R.id.learn_tab).setOnClickListener(this);
		findViewById(R.id.announce_tab).setOnClickListener(this);
		findViewById(R.id.custservice_tab).setOnClickListener(this);
	}

	public void setTitle(String title) {
		mActionbar.setTitle(title);
	}

	public void showProgressDialog(boolean fullScreen) {
		Fragment waitingDialog = getSupportFragmentManager().findFragmentByTag(
				WaitingDialog.TAG);
		if (waitingDialog == null) {
			waitingDialog = new WaitingDialog(fullScreen ? R.color.transparent
					: R.color.white);
		}
		if (fullScreen) {
			((DialogFragment) waitingDialog).show(getSupportFragmentManager(),
					WaitingDialog.TAG);
		} else {
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			transaction.replace(R.id.container_loading, waitingDialog,
					WaitingDialog.TAG);
			transaction.commit();
		}
	}

	public void hideProgressDialog() {
		Fragment waitingDialog = getSupportFragmentManager().findFragmentByTag(
				WaitingDialog.TAG);
		if (waitingDialog != null)
			((DialogFragment) waitingDialog).dismiss();
	}

	public void showAlertDialog(DialogInterface.OnClickListener listener,
			String title, String navigative, String positive, String message,
			boolean cancelable) {

		// dismiss waiting dialog.
		WaitingDialog waitingDialog = (WaitingDialog) getSupportFragmentManager()
				.findFragmentByTag(WaitingDialog.TAG);
		if (waitingDialog != null && waitingDialog.isFullScreen()) {
			waitingDialog.dismiss();
		}

		DialogFragment dialogFragment = new EAlertDialog(listener, title,
				navigative, positive, message);
		dialogFragment.setCancelable(cancelable);
		dialogFragment.show(getSupportFragmentManager(), null);
	}
}
